/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
	"reflect"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
	"k8s.io/utils/ptr"
	"sigs.k8s.io/controller-runtime/pkg/conversion"

	clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
	runtimev1 "sigs.k8s.io/cluster-api/api/runtime/v1beta2"
)

func (src *ExtensionConfig) ConvertTo(dstRaw conversion.Hub) error {
	dst := dstRaw.(*runtimev1.ExtensionConfig)

	return Convert_v1alpha1_ExtensionConfig_To_v1beta2_ExtensionConfig(src, dst, nil)
}

func (dst *ExtensionConfig) ConvertFrom(srcRaw conversion.Hub) error {
	src := srcRaw.(*runtimev1.ExtensionConfig)

	if err := Convert_v1beta2_ExtensionConfig_To_v1alpha1_ExtensionConfig(src, dst, nil); err != nil {
		return err
	}

	dropEmptyStringsExtensionConfig(dst)
	for i, h := range dst.Status.Handlers {
		if h.TimeoutSeconds != nil && *h.TimeoutSeconds == 0 {
			h.TimeoutSeconds = nil
		}
		dst.Status.Handlers[i] = h
	}
	return nil
}

func Convert_v1beta2_ExtensionConfigStatus_To_v1alpha1_ExtensionConfigStatus(in *runtimev1.ExtensionConfigStatus, out *ExtensionConfigStatus, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta2_ExtensionConfigStatus_To_v1alpha1_ExtensionConfigStatus(in, out, s); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1beta2 conditions should not be automatically be converted into legacy conditions (v1beta1).
	out.Conditions = nil

	// Retrieve legacy conditions (v1beta1) from the deprecated field.
	if in.Deprecated != nil && in.Deprecated.V1Beta1 != nil {
		if in.Deprecated.V1Beta1.Conditions != nil {
			clusterv1beta1.Convert_v1beta2_Deprecated_V1Beta1_Conditions_To_v1beta1_Conditions(&in.Deprecated.V1Beta1.Conditions, &out.Conditions)
		}
	}

	// Move new conditions (v1beta2) to the v1beta2 field.
	if in.Conditions == nil {
		return nil
	}
	out.V1Beta2 = &ExtensionConfigV1Beta2Status{}
	out.V1Beta2.Conditions = in.Conditions
	return nil
}

func Convert_v1alpha1_ExtensionConfigStatus_To_v1beta2_ExtensionConfigStatus(in *ExtensionConfigStatus, out *runtimev1.ExtensionConfigStatus, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1alpha1_ExtensionConfigStatus_To_v1beta2_ExtensionConfigStatus(in, out, s); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1beta1 conditions should not be automatically be converted into v1beta2 conditions.
	out.Conditions = nil

	// Retrieve new conditions (v1beta2) from the v1beta2 field.
	if in.V1Beta2 != nil {
		out.Conditions = in.V1Beta2.Conditions
	}

	// Move legacy conditions (v1beta1) to the deprecated field.
	if in.Conditions == nil {
		return nil
	}

	if out.Deprecated == nil {
		out.Deprecated = &runtimev1.ExtensionConfigDeprecatedStatus{}
	}
	if out.Deprecated.V1Beta1 == nil {
		out.Deprecated.V1Beta1 = &runtimev1.ExtensionConfigV1Beta1DeprecatedStatus{}
	}
	if in.Conditions != nil {
		clusterv1beta1.Convert_v1beta1_Conditions_To_v1beta2_Deprecated_V1Beta1_Conditions(&in.Conditions, &out.Deprecated.V1Beta1.Conditions)
	}
	return nil
}

func Convert_v1_Condition_To_v1beta1_Condition(in *metav1.Condition, out *clusterv1beta1.Condition, s apimachineryconversion.Scope) error {
	return clusterv1beta1.Convert_v1_Condition_To_v1beta1_Condition(in, out, s)
}

func Convert_v1beta1_Condition_To_v1_Condition(in *clusterv1beta1.Condition, out *metav1.Condition, s apimachineryconversion.Scope) error {
	return clusterv1beta1.Convert_v1beta1_Condition_To_v1_Condition(in, out, s)
}

func Convert_v1alpha1_ExtensionHandler_To_v1beta2_ExtensionHandler(in *ExtensionHandler, out *runtimev1.ExtensionHandler, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1alpha1_ExtensionHandler_To_v1beta2_ExtensionHandler(in, out, s); err != nil {
		return err
	}

	if in.FailurePolicy != nil {
		out.FailurePolicy = runtimev1.FailurePolicy(*in.FailurePolicy)
	}
	return nil
}

func Convert_v1beta2_ExtensionHandler_To_v1alpha1_ExtensionHandler(in *runtimev1.ExtensionHandler, out *ExtensionHandler, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta2_ExtensionHandler_To_v1alpha1_ExtensionHandler(in, out, s); err != nil {
		return err
	}

	if in.FailurePolicy != "" {
		out.FailurePolicy = ptr.To(FailurePolicy(in.FailurePolicy))
	}
	return nil
}

func Convert_v1alpha1_ClientConfig_To_v1beta2_ClientConfig(in *ClientConfig, out *runtimev1.ClientConfig, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1alpha1_ClientConfig_To_v1beta2_ClientConfig(in, out, s); err != nil {
		return err
	}

	if in.Service != nil {
		if err := Convert_v1alpha1_ServiceReference_To_v1beta2_ServiceReference(in.Service, &out.Service, s); err != nil {
			return err
		}
	}
	return nil
}

func Convert_v1beta2_ClientConfig_To_v1alpha1_ClientConfig(in *runtimev1.ClientConfig, out *ClientConfig, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta2_ClientConfig_To_v1alpha1_ClientConfig(in, out, s); err != nil {
		return err
	}

	if !reflect.DeepEqual(in.Service, runtimev1.ServiceReference{}) {
		out.Service = &ServiceReference{}
		if err := Convert_v1beta2_ServiceReference_To_v1alpha1_ServiceReference(&in.Service, out.Service, s); err != nil {
			return err
		}
	}
	return nil
}

func dropEmptyStringsExtensionConfig(dst *ExtensionConfig) {
	dropEmptyString(&dst.Spec.ClientConfig.URL)
	if dst.Spec.ClientConfig.Service != nil {
		dropEmptyString(&dst.Spec.ClientConfig.Service.Path)
	}
}

func dropEmptyString(s **string) {
	if *s != nil && **s == "" {
		*s = nil
	}
}
